package com.lhkj.ct.meta.admin.common.utils;

import com.lhkj.ct.base.cache.redis.RedisAccess;
import com.lhkj.ct.base.constants.CacheConstants;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.spring.SpringContextHolder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.List;

/**
 * 字典工具类
 *
 *
 */
public class DictUtils {

    /**
     * 分隔符
     */
    public static final String SEPARATOR = ",";

    /**
     * 设置字典缓存
     *
     * @param key 参数键
     * @param dictDatas 字典数据列表
     */
    public static void setDictOptionsCache(String key, List<Option<String>> dictDatas) {
        SpringContextHolder.getBean(RedisAccess.class).setCacheObject(getOptionsCacheKey(key), dictDatas);
    }

    /**
     * 获取字典缓存
     *
     * @param key 参数键
     * @return dictDatas 字典数据列表
     */
    public static List<Option<String>> getDictOptionsCache(String key) {
        return SpringContextHolder.getBean(RedisAccess.class).getCacheObject(getOptionsCacheKey(key));
//        return SpringContextHolder.getBean(RedisAccess.class).getCacheList(getOptionsCacheKey(key));
    }

//    /**
//     * 设置字典缓存
//     *
//     * @param key 参数键
//     * @param dictDatas 字典数据列表
//     */
//    public static void setDictCache(String key, List<TblDictData> dictDatas) {
//        SpringContextHolder.getBean(RedisAccess.class).setCacheList(getCacheKey(key), dictDatas);
//    }
//
//    /**
//     * 获取字典缓存
//     *
//     * @param key 参数键
//     * @return dictDatas 字典数据列表
//     */
//    public static List<TblDictData> getDictCache(String key) {
//        return SpringContextHolder.getBean(RedisAccess.class).getCacheList(getCacheKey(key));
//    }

    /**
     * 根据字典类型和字典值获取字典标签
     *
     * @param dictType 字典类型
     * @param dictValue 字典值
     * @return 字典标签
     */
    public static String getDictLabel(String dictType, String dictValue) {
        return getDictLabel(dictType, dictValue, SEPARATOR);
    }

    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param dictType 字典类型
     * @param dictLabel 字典标签
     * @return 字典值
     */
    public static String getDictValue(String dictType, String dictLabel) {
        return getDictValue(dictType, dictLabel, SEPARATOR);
    }

    /**
     * 根据字典类型和字典值获取字典标签
     *
     * @param dictType 字典类型
     * @param dictValue 字典值
     * @param separator 分隔符
     * @return 字典标签
     */
    public static String getDictLabel(String dictType, String dictValue, String separator) {
        StringBuilder propertyString = new StringBuilder();
        List<Option<String>> dictData = getDictOptionsCache(dictType);
        if (CollectionUtils.isNotEmpty(dictData)) {
            if (StringUtils.containsAny(separator, dictValue)) {
                for (Option<String> dict : dictData) {
                    for (String value : dictValue.split(separator)) {
                        if (value.equals(dict.getValue())) {
                            propertyString.append(dict.getLabel()).append(separator);
                            break;
                        }
                    }
                }
            } else {
                for (Option<String> dict : dictData) {
                    if (dictValue.equals(dict.getValue())) {
                        return dict.getLabel();
                    }
                }
            }
        }
        return StringUtils.stripEnd(propertyString.toString(), separator);
    }

    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param dictType 字典类型
     * @param dictLabel 字典标签
     * @param separator 分隔符
     * @return 字典值
     */
    public static String getDictValue(String dictType, String dictLabel, String separator) {
        StringBuilder propertyString = new StringBuilder();
        List<Option<String>> datas = getDictOptionsCache(dictType);
        if (StringUtils.containsAny(separator, dictLabel) && CollectionUtils.isNotEmpty(datas)) {
            for (Option<String> dict : datas) {
                for (String label : dictLabel.split(separator)) {
                    if (label.equals(dict.getLabel())) {
                        propertyString.append(dict.getValue()).append(separator);
                        break;
                    }
                }
            }
        } else {
            for (Option<String> dict : datas) {
                if (dictLabel.equals(dict.getLabel())) {
                    return dict.getValue();
                }
            }
        }
        return StringUtils.stripEnd(propertyString.toString(), separator);
    }

    /**
     * 删除指定字典缓存
     *
     * @param key 字典键
     */
    public static void removeDictCache(String key) {
        SpringContextHolder.getBean(RedisAccess.class).deleteObject(getCacheKey(key));
    }

    /**
     * 清空字典缓存
     */
    public static void clearDictCache() {
        Collection<String> keys = SpringContextHolder.getBean(RedisAccess.class).keys(CacheConstants.SYS_DICT_OPTION_KEY + "*");
        SpringContextHolder.getBean(RedisAccess.class).deleteObject(keys);
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey) {
        return CacheConstants.SYS_DICT_OPTION_KEY + configKey;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getOptionsCacheKey(String configKey) {
        return CacheConstants.SYS_DICT_OPTION_KEY + configKey;
    }
}
